<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            height: 100px;
            width: 100px;
            background-color: purple;
        }
        
        span {
            position: absolute;
            top: 300px;
            height: 100px;
            width: 100px;
            background-color: purple;
        }
    </style>
    <script src="animate.js"></script>
</head>

<body>
    <button class="btn400">点击</button>
    <button class="btn800">点击</button>
    <div></div>
    <span></span>
    <script>
        var div = document.querySelector("div");
        var span = document.querySelector("span");
        var btn400 = document.querySelector(".btn400");
        var btn800 = document.querySelector(".btn800");
        //调用
        /* animate(div, 400); */
        //当点击按钮 盒子会越跑越快 因为开启了太多的定时器  
        //解决方案让盒子只有一个定时器
        /*  btn.addEventListener("click", function() {
             animate(span, 800);
         }) */
        btn400.addEventListener("click", function() {
            animate(div, 400, function() {
                div.style.backgroundColor = "red";
            });
        })
        btn800.addEventListener("click", function() {
            animate(div, 800, function() {
                div.style.backgroundColor = "pink";
            });
        })
    </script>
</body>

</html>